const chalk = require('chalk');
const semver = require('semver');
const packageConfig = require('../package.json');
const childProcess = require('child_process');
const _ = require('lodash');

const versionRequirements = [
    {
        name: 'node',
        currentVersion: semver.clean(process.version),
        versionRequirement: packageConfig.engines.node
    },
    {
        name: 'npm',
        currentVersion: childProcess.execSync('npm --version').toString().trim(),
        versionRequirement: packageConfig.engines.npm
    }
];

module.exports = function () {
    const warnings = _.chain(versionRequirements).filter(function (mod) {
        return !semver.satisfies(mod.currentVersion, mod.versionRequirement);
    }).map(function (mod) {
        return `  ${mod.name}: ${chalk.red(mod.currentVersion)} should be ${chalk.green(mod.versionRequirement)}`;
    }).value();

    if (warnings.length) {
        _.each(['', chalk.yellow('To use this template, you must update following to modules:'), ''].concat(warnings), function (warning) {
            console.log(warning);
        });
        process.exit(1);
    }
};
